#include "SeqList.h"

void SLCheckCacity(SL* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = realloc(ps->a, newCapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("fail");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newCapacity;

	}	
}


void SLInit(SL* ps)
{
	assert(ps);
	
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;

}


void SLPushBack(SL* ps, SLDataType x)
{
	CheckCacity(ps);

	ps->a[ps->size] = x;
	ps->size++;
}


void SLInsert(SL* ps, int pos, SLDataType x)
{
	SLCheckCacity(ps);

	int end = ps->size - 1;
	while (end >= pos)
	{
		ps->a[end + 1] = ps->a[end];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;
}

void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos>=0);
	aseert(pos<ps->size);
	int begin = pos + 1;
	while (begin <= ps->size-1)
	{
		ps->a[begin - 1] = ps->a[begin];
		begin++;
	}
	ps->size--;
}

int SLFind(SL* ps,SLDataType x,int begin)
{

	assert(ps);
	for (int i = begin;i<ps->size;i++)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
	return -1;

}




